//index.js
var util = require('../../../utils/util.js')
const app = getApp()
var template = require('../tabbar/template.js');

Page({
  data: {
   
    userInfo: {},
   
    stv: {
      windowWidth: 0,
      lineWidth: 0,
      offset: 0,
      tStart: false
    },
    activeTab: 0,
    loadingStatus: false,
    orderList:[],
    isEnd:false,
    isShow:false
  },
  
  
  onShow(){
    let  that = this;
    wx.hideHomeButton()
    that.setData({
      isShow:true
    })
    // 获取订单列表

    this.getOrderList()
  },
  onLoad: function () {
    template.tabbar("tabBar", 3, this)//0表示第一个tabbar
   
  },
  
 
  //列表
  getOrderList: function () {
    
    var that = this;
    wx.cloud.callFunction({
      // 云函数名称
      name: 'crud',
      // 传给云函数的参数
      data: {
        db:"orders",
        type:"getall",
        data:"",
        indexKey:"",
        //待筛选的参数-库存 >0
        commandtype:[],
        condition:{
          status:1,
          deliveredMan:wx.getStorageSync('openid')
        },
      },
    })
    .then(res => {
      if(res.result){
        if (res.result.data.length > 0) {
           
          
          that.setData({
            orderList:res.result.data,
            isEnd:true,
            isShow:false
          })
          return
    } else{
      console.log('orderList not exist')
      that.setData({
        orderList: null,
        isShow:false
        });
      }
      return
      
      }else{
        that.setData({
          orderList: null,
          isShow:false
        });
      }
    })
    .catch(console.error)  
    
  },
  //详情
  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  //完成配送
  tocomplete(e){
    const that = this;
    const orderId = e.currentTarget.dataset.id;
  
    wx.showModal({
      title: '确定已完成配送吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          const db = wx.cloud.database()
        
          db.collection('orders').doc(orderId).update({
            data: {
              delivered: 2,
              status:2,
              logisticsStatus:1
            },
            success: function(res) {
              console.log(res)
              that.getOrderList();

            }
          })
         
          that.getOrderList();
        }
      }
    })
  },

  swiperchange: function (e) {
    //console.log('swiperCurrent',e.detail.current)
    let { tabs, stv, activeTab } = this.data;
    activeTab = e.detail.current;
    this.setData({ activeTab: activeTab })
    stv.offset = stv.windowWidth * activeTab;
    this.setData({ stv: this.data.stv })
  },
  handlerEnd(e) {
    console.log('handlerEnd')
    let { clientX, clientY } = e.changedTouches[0];
    let endTime = e.timeStamp;
    let { tabs, stv, activeTab } = this.data;
    console.log(activeTab)
    let { offset, windowWidth } = stv;
    //快速滑动
    if (endTime - this.tapStartTime <= 300) {
      console.log('快速滑动')
      //判断是否左右滑动(竖直方向滑动小于50)
      if (Math.abs(this.tapStartY - clientY) < 50) {
        //Y距离小于50 所以用户是左右滑动
        console.log('竖直滑动距离小于50')
        if (this.tapStartX - clientX > 5) {
          //向左滑动超过5个单位，activeTab增加
          console.log('向左滑动')
          if (activeTab < this.tabsCount - 1) {
            this.setData({ activeTab: ++activeTab })
          }
        } else if (clientX - this.tapStartX > 5) {
          //向右滑动超过5个单位，activeTab减少
          console.log('向右滑动')
          if (activeTab > 0) {
            this.setData({ activeTab: --activeTab })
          }
        }
        stv.offset = stv.windowWidth * activeTab;
      } else {
        //Y距离大于50 所以用户是上下滑动
        console.log('竖直滑动距离大于50')
        let page = Math.round(offset / windowWidth);
        if (activeTab != page) {
          this.setData({ activeTab: page })
        }
        stv.offset = stv.windowWidth * page;
      }
    } else {
      let page = Math.round(offset / windowWidth);
      if (activeTab != page) {
        this.setData({ activeTab: page })
      }
      stv.offset = stv.windowWidth * page;
    }
    stv.tStart = false;
    this.setData({ stv: this.data.stv })
  },
  handlerStart(e) {
    console.log('handlerStart')
    let { clientX, clientY } = e.touches[0];
    this.startX = clientX;
    this.tapStartX = clientX;
    this.tapStartY = clientY;
    this.data.stv.tStart = true;
    this.tapStartTime = e.timeStamp;
    this.setData({ stv: this.data.stv })
  },
  handlerMove(e) {
    console.log('handlerMove')
    let { clientX, clientY } = e.touches[0];
    let { stv } = this.data;
    let offsetX = this.startX - clientX;
    this.startX = clientX;
    stv.offset += offsetX;
    if (stv.offset <= 0) {
      stv.offset = 0;
    } else if (stv.offset >= stv.windowWidth * (this.tabsCount - 1)) {
      stv.offset = stv.windowWidth * (this.tabsCount - 1);
    }
    this.setData({ stv: stv });
  },
  onPullDownRefresh: function () {
    let that = this;
    that.getOrderList()
   },
})
